package so.ifl.manager.staff;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.CoachCardService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.CoachCardVo;

/**
 *create by aaron.lu
 *create at 2013-2-27 下午11:20:14
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("coachCardManager")
public class CoachCardManager {
	
	@Autowired
	private CoachCardService coachCardService;
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 查询教练证信息
	 * create by aaron.lu
	 * create at 2013-2-27 下午11:27:50
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getCoachCard(Page<CoachCardVo> page, Map<String, Object> params){
		logger_.debug("getCoachCard params:" + params);
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			
			
			int offset = page.getFirstEntityIndex();
			int limit = page.getPageSize();
			List<CoachCardVo> list = coachCardService.queryCoachCardWithParamAndPagination(params, offset, limit);
			Integer total = coachCardService.countCoachCardWithParam(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<CoachCardVo>());
			page.setEntityCount(0);
		}
		
	}
	
	/**
	 * 更新教练证状态
	 * create by aaron.lu
	 * create at 2013-2-27 下午11:32:21
	 * <mailto: atyuan.lu@gmail.com>
	 * @param coachCardVos
	 *
	 */
	@DataResolver
	public void saveCoachCard(Collection<CoachCardVo> coachCardVos) {

		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null != accountVo.getAccoutId()) {
			for (CoachCardVo coachCardVo : coachCardVos) {
				EntityState entityState = EntityUtils.getState(coachCardVo);
				coachCardVo.setCompanyId(accountVo.getCompanyId());//公司数据域
				if (EntityState.NEW.equals(entityState)) {
					// 新增
					logger_.debug("NEW:" + coachCardVo);
					coachCardService.insertCoachCard(coachCardVo);
				} else if (EntityState.MODIFIED.equals(entityState)) {
					// 修改
					logger_.debug("MODIFY:" + coachCardVo);
					coachCardService.updateCoachCard(coachCardVo);
				} else if (EntityState.DELETED.equals(entityState)) {
					// 删除
					logger_.debug("DELETE:" + coachCardVo);
					coachCardService.deleteCoachCard(coachCardVo
							.getCoachCardId());
				}
			}
		} else {
			throw new SessionTimeOutException("登录超时。。");
		}

	}
}
